//
//  UIImage+Extension.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/13.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit

extension UIImage{
    class func screenShotEffectImg()->(UIImage){
        ////     开启的图片上下文,此种更为清晰
        //        UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, true, 0)
        //        开启上下文
        UIGraphicsBeginImageContext(UIScreen.mainScreen().bounds.size)
        
        let context = UIGraphicsGetCurrentContext()
        
        let layer = UIApplication.sharedApplication().keyWindow!.layer
        layer.renderInContext(context!)
        //        drawLayer(layer, inContext: context!)
        
        let img =  UIGraphicsGetImageFromCurrentImageContext()
        
        
        
        //       UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil)
        
        //      关闭上下文
        UIGraphicsEndImageContext()
        //        处理成为毛玻璃图片
        return  img.applyLightEffect()
    }
    /**
     等比例缩放图片,产生一张新图片
     
     - parameter width: 图片缩放成为的宽度
     */
    func scaleImage(scaleWidth:CGFloat)->UIImage{
        
     let scaleHeight = scaleWidth / size.width  * size.height
        
//        开启上下文
        UIGraphicsBeginImageContext(CGSizeMake(scaleWidth, scaleHeight))
        
        drawInRect(CGRect(x: 0, y: 0, width: scaleWidth, height: scaleHeight))
        
       let scaleImage = UIGraphicsGetImageFromCurrentImageContext()
       let date =  UIImagePNGRepresentation(scaleImage)!
        date.writeToFile("/Users/simon/Desktop/图片.png", atomically: true)
//        关闭上下文
       UIGraphicsEndImageContext()
        
        return scaleImage
    
    }
}
